package cn.edu.ctbu.sbadmin.grade.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import oshi.util.Util;

public class GenderConverter implements Converter<Integer> {
    //重载支持的java数据类型格式为Integer
    @Override
    public Class<?> supportJavaTypeKey(){
        return Integer.class;
    }
    //重载支持的Excel代码为String型
    @Override
    public CellDataTypeEnum supportExcelTypeKey(){
        return CellDataTypeEnum.STRING;
    }
    /*.**
    * @Description:  将用户由excel格式导入的数据转换为java格式的Integer
    * @Param:
    * @Return:
    * @Author: HighJ
    * @Date Created in
    */

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception{
        String cellstr = context.getReadCellData().getStringValue();
        if(Util.isBlank(cellstr))
            return null;
        if("男".equals(cellstr)){
            return 1;
        }
        else if("女".equals(cellstr)){
            return 2;
        }
        else {
            return 0;
        }
    }

    /*.**
    * @Description:将需要导出的性别的数据由Integer转换为Excel中的字符型转换为
    * @Param:
    * @Return:
    * @Author: HighJ
    * @Date Created in
    */

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context)throws Exception{
        Integer cellvalue = context.getValue();
        if(cellvalue == null)
            return new WriteCellData<>("");
        else if(cellvalue == 1)
            return new WriteCellData<>("男");
        else if(cellvalue == 2)
            return new WriteCellData<>("女");
        else
            return new WriteCellData<>("不详");
    }
}
